package org.lzuploader.gui.listeners.main;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.program.Program;
import org.lzuploader.gui.helpers.EasyMessageBox;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;

/** Class used when the user clicks on an launch wow widget.
 * 
 * @author Emmanuel Cron
 *
 */
public class LaunchSelectionListener implements SelectionListener
{
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(LaunchSelectionListener.class);
    
    /** Localization of this class. */
    private static Localization localization = Localization.getInstance();

    /** Used when widget is selected by default.
     * @param e Associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);
    }

    /** Used when widget is manually selected.
     * @param e Associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("Trying to launch WoW.");
        
        final Config config = Config.getInstance();
        
        if (config.getWowPath() == null)
        {
            log.trace("Config does not contain WoW path. Displaying info window.");
            
            EasyMessageBox.info(
                localization.getString("DIALOG_TITLE_INFO"),
                localization.getString("DIALOG_NO_WOW_LOCATION")
            );
        }
        else
        {
            log.trace("Checking if Launcher.exe can be found.");
            
            final File file = new File(config.getWowPath() + "Launcher.exe");
            
            if (!file.exists())
            {
                log.debug("Launcher.exe could not be found. Displaying error window.");
                
                EasyMessageBox.error(
                    localization.getString("DIALOG_TITLE_ERROR"),
                    localization.getString("DIALOG_CANT_FIND_WOW")
                );
            }
            else
            {
                log.debug("Launcher.exe found. Launching program.");
                
                Program.launch(file.getAbsolutePath());
            }
        }
    }
}
